﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using WMSWebApi.Interface;
using WMSWebApi.Models;

namespace WMSWebApi.Implement
{
    /// <summary>
    ///实现工位接口
    /// </summary>
    public class ImpStations : IStations
    {
        WMSDATAContext _context = new WMSDATAContext();
        List<StationsTable> list;
        public ImpStations()
        {
            list = _context.StationsTables.ToList<StationsTable>();
        }
        /// <summary>
        /// 添加工位
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        public int Add(StationsTable item)
        {
            if (item == null)
            {
                throw new ArgumentNullException("item");
            }
            var station = new StationsTable
            {
                StationName = item.StationName,
                StationNumber = item.StationNumber,
                StationStatus = item.StationStatus
            };
            _context.StationsTables.Add(station);
            return _context.SaveChanges();
        }
        /// <summary>
        /// 删除工位
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int Delete(int id)
        {
            var station = _context.StationsTables.First(s=>s.Id==id);
            _context.StationsTables.Remove(station);
            return _context.SaveChanges();
        }
        /// <summary>
        /// 查询单个工位
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public int Get(string name)
        {
            StationsTable station= list.Find(s=>s.StationName==name);
            return (int)station.StationNumber;
        }
        /// <summary>
        /// 查询所有工位
        /// </summary>
        /// <returns></returns>
        public IEnumerable<StationsTable> GetAll()
        {
            return list;
        }
        /// <summary>
        /// 查询所有工位名称
        /// </summary>
        /// <returns></returns>
        public List<string> GetAllName()
        {
            List<string> name = new List<string>();
            foreach (var item in list)
            {
                name.Add(item.StationName);
            }
            return name;
        }
        /// <summary>
        /// 修改工位
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        public int Update(StationsTable item)
        {
            var station = new StationsTable 
            {
                StationName = item.StationName,
                StationNumber = item.StationNumber,
                StationStatus = item.StationStatus
            };
            _context.StationsTables.Update(station);
            return _context.SaveChanges();
             
        }
    }
}
